import os
from datetime import timedelta
from flask import Flask,session

app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)
#因为session是加密的需要设定好加密规则
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=2)
#session.permanent = True 表名持久性
#PERMANENT_SESSION_LIFETIME 表示设置session生存时间

@app.route('/')
def index():
    session['username'] = 'kangbazi'
    session['user_id'] = '666'
    session.permanent = True
    print(type(session))
    return 'Hello World!'

@app.route('/get_session/')
def get_session():
    username = session.get("username")
    user_id = session.get("user_id")
    print(user_id)
    return username or '没有session'


@app.route('/del_session/')
def del_session():
    # session.pop('username')
    session.clear()
    return '删除成功'

if __name__ == '__main__':
    app.run()
